<template>
  <a-drawer
    title="新增考试科目信息"
    :width="windowWidth"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="testManageEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form >
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
                <a-input placeholder="请输入..." v-model="postParams.dwmc" disabled/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-input placeholder="请输入..." v-model="postParams.zymc" disabled/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-input placeholder="请输入..." v-model="postParams.yjfxmc" disabled/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.dsxxmId">
                <a-select-option v-for="i in selectMenu.ds" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.kmlxm" @change="kmChange">
                <a-select-option v-for="i in TYPE0392" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目编码">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.bsckkmId">
                <a-select-option v-for="i in selectMenu.bm" :key="i.id" :value="i.id">{{ i.kmbm+'-'+i.kmmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0392']
export default {
  name: 'TestManageEdit',
  props: {
    testManageEditVisiable: {
      default: false
    },
    info: {
      default: () => ({})
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      windowWidth:"450",
      loading: false,
      postParams: {
        ssyx: '',
        zy: '',
        yjfx: '',
        ds: '',
        kmlx: '',
        kmbm: ''
      },
      selectMenu: {
        ds:[],
        bm:[]
      },
      doctoralAdmissionYear:''
    }
  },
  watch: {
    info () {
      this.postParams = this.info
      this.postParams.dwmc=this.postParams.dwh+'-'+this.postParams.dwmc;
      this.postParams.zymc=this.postParams.zyh+'-'+this.postParams.zymc;
       this.initSelectMenu()
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0392: state => state.app['TYPE0392'] // 年级
    })
  },
  mounted(){
     this.windowWidth = document.body.clientWidth>450?450:document.body.clientWidth
  },
  created () {
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
     this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
      })
  },
  methods: {
    initSelectMenu () {
      let params={yjfxm:this.postParams.yjfxm,zsxn:this.postParams.zsxn,zym:this.postParams.zyh,ssyxm:this.postParams.dwh}
      this.$api.base.getGuide(params).then(res=>{
       this.$set(this.selectMenu,'ds',res.data)
      })
       let param={
        ssyxm:this.postParams.dwh,
        kmlxm:this.postParams.kmlxm
      }
      this.$api.base.getCode(param).then(res=>{
        this.$set(this.selectMenu,'bm',res.data)
      })
    },
    reset () {
      this.data = []
    },
    // 选择科目类型
    kmChange(code){
      this.$set(this.postParams,'bsckkmId','')
      this.$set(this.selectMenu,'bm','')
      if(code){
      let params={
        ssyxm:this.postParams.ssyxm,
        kmlxm:this.postParams.kmlxm
      }
      this.$api.base.getCode(params).then(res=>{
        this.$set(this.selectMenu,'bm',res.data)
      })
      }
    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
      this.$api.doctorRecruit
        .examinationUpdate({
          id:this.postParams.id,
          bsckkmId:this.postParams.bsckkmId,
          dsxxmId:this.postParams.dsxxmId,
          kmlxm:this.postParams.kmlxm,
          yjfxm:this.postParams.yjfxm
        })
        .then(res => {
          // this.$message.success(res.msg)
          this.$emit("success");
        })
        .finally(() => {
          this.loadingBtn = false;
           this.form.resetFields()
        });
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
